home *** CD-ROM | disk | FTP | other *** search
- //: C22:MultipleInheritance4.cpp
- // From Thinking in C++, 2nd Edition
- // Available at http://www.BruceEckel.com
- // (c) Bruce Eckel 1999
- // Copyright notice in Copyright.txt
- // "Tying off" virtual bases
- // so you don't have to worry about them
- // in derived classes
- #include "../purge.h"
- #include <iostream>
- #include <vector>
- using namespace std;
-
- class MBase {
- public:
- // Default constructor removes responsibility:
- MBase(int = 0) {}
- virtual char* vf() const = 0;
- virtual ~MBase() {}
- };
-
- class D1 : virtual public MBase {
- public:
- D1() : MBase(1) {}
- char* vf() const { return "D1"; }
- };
-
- class D2 : virtual public MBase {
- public:
- D2() : MBase(2) {}
- char* vf() const { return "D2"; }
- };
-
- class MI : public D1, public D2 {
- public:
- MI() {} // Calls default constructor for MBase
- char* vf() const {
- return D1::vf(); // MUST disambiguate
- }
- };
-
- class X : public MI {
- public:
- X() {} // Calls default constructor for MBase
- };
-
- int main() {
- vector<MBase*> b;
- b.push_back(new D1);
- b.push_back(new D2);
- b.push_back(new MI); // OK
- b.push_back(new X);
- for(int i = 0; i < b.size(); i++)
- cout << b[i]->vf() << endl;
- purge(b);
- } ///:~
-